<?php
/**
 * Controller_Ajax_City
 *
 * Класс Controller_Ajax_City 
 */
class Controller_Ajax_City extends Controller_Ajax {

	/**
	 * (non-PHPdoc)
	 *
	 */
	public function actionSwitch() {
	  Service_Debug::setDebug();
		switch ($this->arrRequest[2]) {
			case 'get':
				$this->actionGetCities();
				break;
			case 'is_exist':
				$this->actioIsExist();
				break;
			default: $this->setError(404);
 		}
	}

	
	/**
	 * Получает список городов по маске
	 *
	 */
	public function actionGetCities () {
		Service_Debug::setDebug();
		$nameStartWith = isset($_POST['nameStartWith']) ? Service_Escape::absolutelyEscapeString($_POST['nameStartWith']) : '';
		
		if (empty($nameStartWith)) return $this->makeStandardError('Не получены данные');
		$objCity = new City();
		$citiesArray = $objCity->find('all',array('select' => '`city`.`city_name`,`city`.`ID_city`', 'conditions' => "`city`.`city_name` LIKE '%" . $nameStartWith . "%' AND `city`.`no_on_map`=0", 'limit' => '5'));
		$this->arrReturn = array('citiesNames' => $citiesArray );
	}
	
	/**
	 * Проверяет существование города
	 *
	 */
	public function actioIsExist () {
		Service_Debug::setDebug();
		$cityName = isset($_POST['cityName']) ? Service_Escape::absolutelyEscapeString($_POST['cityName']) : '';
	
		if (empty($cityName)) return $this->makeStandardError('Не получены данные');
		$objCity = new City();
		$citiyId = $objCity->getIdByName($cityName);
		if($citiyId) $this->arrReturn = array('result' => 'OK');
		else $this->arrReturn = array('result' => 'NO');
	}
}
?>